﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using BaseData;
using BaseData.Exts;
using BaseLib.Helper;
using BaseUtility;
using Newtonsoft.Json;
namespace WebUI.Ashx.App
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : Base, IHttpHandler
    {
        private readonly Enums.AccType _accType = new GetAccType().Parse(Request.QueryString["url"]);
        public void ProcessRequest(HttpContext context)
        {
            var msg = new MsgErr { };
            try
            {
                var flag = 0;
                var userName = (context.Request.Form["Name"] ?? "").Trim().ToLower(); if (userName.IsNullOrWhiteSpace()) flag++;
                var userPass = (context.Request.Form["Pass"] ?? "").Trim(); if (userPass.IsNullOrWhiteSpace()) flag++;
                if (flag != 0) msg = new MsgErr { error = 1, message = new StringBuilder("用户名或密码为空！"), };
                using (var ctx = new ContextExt())
                {
                    IQueryable<AccClass> sql;
                    switch (_accType)
                    {
                        case Enums.AccType.Staff:
                            sql = ctx.Web_Base_Account_Staff.Select(q => new AccClass
                            {
                                Id = q.PkId,
                                Name = q.Name,
                                AccPass = q.Pass,
                                FlagStatus = q.FlagStatus,
                                Type = _accType,
                            });
                            break;
                        case Enums.AccType.Super:
                            sql = ctx.Web_Base_Account_Super.Select(q => new AccClass
                            {
                                Id = q.PkId,
                                Name = q.Name,
                                AccPass = q.Pass,
                                FlagStatus = q.FlagStatus,
                                Type = _accType,
                            });
                            break;
                        case Enums.AccType.Member:
                            sql = ctx.Web_Base_Account_Member.Select(q => new AccClass
                            {
                                Id = q.PkId,
                                Name = q.Name,
                                AccPass = q.Pass,
                                FlagStatus = q.FlagStatus,
                                Type = _accType,
                            });
                            break;
                        case Enums.AccType.Vip:
                            sql = ctx.Web_Base_Account_Vip.Select(q => new AccClass
                            {
                                Id = q.PkId,
                                Name = q.Name,
                                AccPass = q.Pass,
                                FlagStatus = q.FlagStatus,
                                Type = _accType,
                            });
                            break;
                        case Enums.AccType.Biz:
                            sql = ctx.Web_Base_Account_Biz.Select(q => new AccClass
                            {
                                Id = q.PkId,
                                Name = q.Name,
                                AccPass = q.Pass,
                                FlagStatus = q.FlagStatus,
                                Type = _accType,
                            });
                            break;
                        case Enums.AccType.User:
                        default:
                            sql = ctx.Web_Base_Account_User.Select(q => new AccClass
                            {
                                Id = q.PkId,
                                Name = q.Name,
                                AccPass = q.Pass,
                                FlagStatus = q.FlagStatus,
                                Type = _accType,
                            });
                            break;
                    }
                    sql = sql.Where(w => w.Name != null && w.Name != "" && w.Name == userName && w.AccPass != null && w.AccPass != "" && w.AccPass == userPass);
                    // context.Response.Write("Name：" + Name + "\r\nuserPass：" + userPass + "\r\n" + sql + "\r\n" + sql.Any()); return;
                    if (sql.Any())
                    {
                        var item = sql.FirstOrDefault();
                        if (item != null)
                        {
                            var status = (Enums.UserStatus)Enum.Parse(typeof(Enums.UserStatus), value: (item.FlagStatus ?? 0).ToString(CultureInfo.InvariantCulture));
                            switch (status)
                            {
                                case Enums.UserStatus.WhiteName:
                                    msg = new MsgErr { error = 0, message = new StringBuilder("白名单用户登录成功"), };
                                    break;
                                case Enums.UserStatus.Normal:
                                    msg = new MsgErr { error = 0, message = new StringBuilder("正常的用户登录成功"), };
                                    break;
                                case Enums.UserStatus.Stop:
                                    msg = new MsgErr { error = 1, message = new StringBuilder("停用的用户登录失败"), };
                                    break;
                                case Enums.UserStatus.Delete:
                                    msg = new MsgErr { error = 1, message = new StringBuilder("删除的用户登录失败"), };
                                    break;
                                case Enums.UserStatus.Out:
                                    msg = new MsgErr { error = 1, message = new StringBuilder("出局的用户登录失败"), };
                                    break;
                                case Enums.UserStatus.BlackName:
                                    msg = new MsgErr { error = 1, message = new StringBuilder("黑名单用户登录失败"), };
                                    break;
                                default:
                                    msg = new MsgErr { error = 1, message = new StringBuilder("未经认证的用户登录失败"), };
                                    break;
                            }
                            if (msg.error == 0)
                            {
                                AccCookies.Set(item, _accType, true, false);
                                msg.title = "提示信息";
                            }
                            msg.id = item.Id;
                        }
                    }
                    else
                    {
                        msg = new MsgErr { error = 1, message = new StringBuilder("登录失败，用户名[" + userName + "]不存在或密码[" + userPass + "]错误"), memo = _accType.ToString(), };
                    }
                }
            }
            catch (Exception err)
            {
                msg = new MsgErr { error = 1, message = new StringBuilder(err.Message), };
            }
            if (msg.error == 1)
            {
                AccCookies.Clear(_accType);
                msg.title = "错误信息";
                msg.id = 0;
            }
            msg.account = _accType.ToString();
            context.Response.Write(msg.ObjectsToJson());
            context.Response.ContentType = "text/plain";
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}